package xin.fms.controller;


import com.alibaba.fastjson.JSONObject;
import com.sun.jersey.api.client.Client;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import xin.common.file.RemoteUploader;
import xin.common.file.UploadResult;
import xin.common.ueditor.ActionEnter;
import xin.common.ueditor.ActionType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * <pre>
 * UEditor Controller 控制器
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/9/20
 */
@Controller
@Api(value = "xin-ueditor", description = "富文本编辑器", position = 2)
@RequestMapping("/ueditor")
@Slf4j
public class UEditorController {

    //远程文件服务器 http://ip:端口
    private String uploadHost = "";

    //各文件上传前缀
    private String imagePath = "image";
    private String filePath = "file";
    private String videoPath = "video";

    @ApiOperation(value = "富文本编辑器", notes = "富文本编辑器", response = JSONObject.class)
    @RequestMapping(value="/upload", method={RequestMethod.GET, RequestMethod.POST})
    public void config(HttpServletRequest request,HttpServletResponse response,@RequestParam("action") String action){
        response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        try {
            String exec = new ActionEnter(request, rootPath).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
            // 初始化
//            if(ActionType.CONFIG.equals(action)){
//                ActionEnter actionEnter = new ActionEnter(request, rootPath);
//                render(response, actionEnter.exec());
//            }
//            else {
//                MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//                MultipartHttpServletRequest Murequest = resolver.resolveMultipart(request);
//                Map<String, MultipartFile> files = Murequest.getFileMap();//得到文件map对象
//                String path = imagePath;
//
//                //适配acton type
//                if(ActionType.UPLOAD_IMAGE.equals(action)){
//                    path = imagePath;
//                }else if(ActionType.UPLOAD_VIDEO.equals(action)){
//                    path = videoPath;
//                }else if(ActionType.UPLOAD_SCRAWL.equals(action)){
//                    path = imagePath;
//                }else if(ActionType.UPLOAD_FILE.equals(action)){
//                    path = filePath;
//                }else if(ActionType.CATCH_IMAGE.equals(action)){
//                    path = imagePath;
//                }else if(ActionType.LIST_IMAGE.equals(action)){
//                    path = imagePath;
//                }
//
//                // TODO 这里的对于图片列表的上传需要有所修改
//                for(MultipartFile multipartFile: files.values()){
//                    //设置文件属性  为以后持久化留口
//                    UploadResult result = new UploadResult();
//                    result.setOriginalFilename(multipartFile.getOriginalFilename());
//                    result.setSize(multipartFile.getSize());
//                    result.setType(FilenameUtils.getExtension(multipartFile.getOriginalFilename()));
//
//                    //文件上传
//                    RemoteUploader uploader = new RemoteUploader();
//                    uploader.upload(multipartFile, request, response, uploadHost, path,result);
//
//                    //数据返回
//                    render(response,JSONObject.toJSONString(result));
//                }
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    private void render(HttpServletResponse response,String json) throws IOException {
//        PrintWriter writer = response.getWriter();
//        writer.write(json);
//        writer.flush();
//        writer.close();
//    }
}
